package com.tgu.home_action.service.impl;

import com.tgu.home_action.dao.VolunteerInformationMapper;
import com.tgu.home_action.dao.VolunteerMapper;
import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.Volunteer;
import com.tgu.home_action.pojo.VolunteerInformation;
import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.VolunteerInformationService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Validated
@Service
public class VolunteerInformationServiceImpl implements VolunteerInformationService {
    @Autowired
    private VolunteerMapper volunteerMapper;
    @Autowired
    private VolunteerInformationMapper volunteerInformationMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询个人信息
     */
    @Override
    public ResponseData<Volunteer> getInformation(Volunteer volunteerUser) {
        Volunteer volunteer = volunteerMapper.getById(volunteerUser.getId());
        VolunteerInformation volunteerInformation = volunteerInformationMapper.getInformationByVolunteerId(volunteerUser.getId());
        volunteer.setVolunteerInformation(volunteerInformation);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), volunteer);
    }

    /**
     * 绑定志愿者信息
     */
    @Override
    public ResponseData<VolunteerInformation> bindInformation(@NotNull(message = "name不能为空") String name, @NotNull(message = "IDCard不能为空") @Length(min = 18, max = 18, message = "IDCard长度必须是18") String idCard, Volunteer volunteerUser) {
        VolunteerInformation volunteerInformation = new VolunteerInformation(name, idCard);
        volunteerInformation = volunteerInformationMapper.getInformation(volunteerInformation);
        if (volunteerInformation == null) {
            return new ResponseData<>(ResponseState.VOLUNTEER_NOT_EXIST.getMessage(), ResponseState.VOLUNTEER_NOT_EXIST.getValue());
        }
        if (volunteerInformation.getVolunteerId() != null) {
            return new ResponseData<>(ResponseState.VOLUNTEER_INFORMATION_HAS_BIND.getMessage(), ResponseState.VOLUNTEER_INFORMATION_HAS_BIND.getValue());
        }
        volunteerInformation.setRegisterTime(new Timestamp(new Date().getTime()));
        volunteerInformation.setVolunteerId(volunteerUser.getId());
        volunteerInformationMapper.bindVolunteer(volunteerInformation);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), volunteerInformation);
    }

    /**
     * 修改个人信息
     */
    @Override
    public ResponseData<Object> updateInformation(VolunteerInformation volunteerInformation, Volunteer volunteerUser) {
        volunteerInformation.setVolunteerId(volunteerUser.getId());
        Boolean isSuccess = volunteerInformationMapper.updateInformation(volunteerInformation);
        if (isSuccess) {
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
        } else {
            return new ResponseData<>(ResponseState.VOLUNTEER_NOT_BIND.getMessage(), ResponseState.VOLUNTEER_NOT_BIND.getValue());
        }
    }

    /**
     * 获取解除绑定的验证码
     */
    @Override
    public ResponseData<Object> getUnbindCode(Volunteer volunteerUser) {
        volunteerUser = volunteerMapper.getById(volunteerUser.getId());
        if (volunteerUser.getPhone() == null) {
            return new ResponseData<>(ResponseState.PHONE_NOT_BIND.getMessage(), ResponseState.PHONE_NOT_BIND.getValue());
        }
        stringRedisTemplate.opsForValue().set(RedisHeader.VOLUNTEER_UNBIND_CODE.getHeader() + volunteerUser.getPhone() + volunteerUser.getId(), "123456");
        stringRedisTemplate.expire(RedisHeader.VOLUNTEER_UNBIND_CODE.getHeader() + volunteerUser.getPhone() + volunteerUser.getId(), 5, TimeUnit.MINUTES);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 查询所有志愿者
     */
    @Override
    public ResponseData<List<VolunteerInformation>> getAllVolunteer(VolunteerInformation volunteerInformation) {
        List<VolunteerInformation> volunteerInformationList = (List<VolunteerInformation>) volunteerInformationMapper.getAllVolunteer(volunteerInformation);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), volunteerInformationList);

    }

    /**
     * 通过id查询volunteer
     */
    @Override
    public ResponseData<VolunteerInformation> getVolunteerById(int informationId) {
        VolunteerInformation volunteerInformation = volunteerInformationMapper.getVolunteerById(informationId);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), volunteerInformation);
    }

    /**
     * 解除绑定
     */
    @Override
    public ResponseData<Object> unbindInformation(@NotNull(message = "code不能为空") String code, Volunteer volunteerUser) {
        volunteerUser = volunteerMapper.getById(volunteerUser.getId());
        if (volunteerUser.getPhone() == null) {
            return new ResponseData<>(ResponseState.PHONE_NOT_BIND.getMessage(), ResponseState.PHONE_NOT_BIND.getValue());
        }
        String rightCode = stringRedisTemplate.opsForValue().get(RedisHeader.VOLUNTEER_UNBIND_CODE.getHeader() + volunteerUser.getPhone() + volunteerUser.getId());
        if (rightCode == null) {
            return new ResponseData<>(ResponseState.CODE_NOT_EXIST.getMessage(), ResponseState.CODE_NOT_EXIST.getValue());
        }
        if (!rightCode.equals(code)) {
            return new ResponseData<>(ResponseState.CODE_IS_ERROR.getMessage(), ResponseState.CODE_IS_ERROR.getValue());
        }
        VolunteerInformation volunteerInformation = volunteerInformationMapper.getInformationByVolunteerId(volunteerUser.getId());
        if (volunteerInformation == null) {
            return new ResponseData<>(ResponseState.VOLUNTEER_NOT_BIND.getMessage(), ResponseState.VOLUNTEER_NOT_BIND.getValue());
        }
        volunteerInformationMapper.unbindVolunteer(volunteerInformation.getId());
        stringRedisTemplate.delete(RedisHeader.VOLUNTEER_UNBIND_CODE.getHeader() + volunteerUser.getPhone() + volunteerUser.getId());
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }


}
